package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.StaticConfig;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.RegisterVo;
import com.dtsz.m2.utils.MyCountdownUtils;
import com.dtsz.m2.utils.RSAEncrypt;
import com.dtsz.m2.utils.RegluarUtils;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

public class RetrievePwdActivity extends BaseActivity {
    private Button btVerification;
    private EditText etPhone, etCode, etPassword;
    private MyCountdownUtils myCountdownUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_retrieve_pwd);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        etPhone = findViewById(R.id.et_phone);
        etCode = findViewById(R.id.et_code);
        etPassword = findViewById(R.id.et_password);
        btVerification = findViewById(R.id.bt_verification);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.tv_break).setOnClickListener(this);
        findViewById(R.id.bt_pwd).setOnClickListener(this);
        btVerification.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_break:
                finish();
                break;
            case R.id.bt_pwd:
                String phone = etPhone.getText().toString();
                if (!RegluarUtils.isMobile(phone)) {
                    showToast("手机号码格式错误");
                    return;
                }
                String code = etCode.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }
                String pwd = etPassword.getText().toString();
                if (!RegluarUtils.isLetterDigit(pwd)) {
                    showToast("密码长度必须8~16位，并且包含字母和数字");
                    return;
                }

                RegisterVo registerVo = new RegisterVo();
                registerVo.setPhone(phone);
                registerVo.setCode(code);
                registerVo.setPassword(RSAEncrypt.encrypt(pwd));
                getClick(registerVo);
                break;
            case R.id.bt_verification:
                getCode();
                break;
        }
    }

    private void getClick(RegisterVo registerVo) {
        loading();
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.GETPASSWORD_URL,
                hucParameInterfase.getRequestParameter(registerVo), true);
    }

    private void getCode() {
        String phone = etPhone.getText().toString();
        if (!RegluarUtils.isMobile(phone)) {
            showToast("手机号码格式错误");
            return;
        }
        loading();
        RegisterVo registerVo = new RegisterVo();
        registerVo.setPhone(phone);
        registerVo.setType(StaticConfig.MSGCODE);
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_2, Request.Method.POST, UrlConfig.CODE_URL,
                hucParameInterfase.getRequestParameter(registerVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String msg = (String) response;
                if ("保存成功！".equals(msg)) {
                    finish();
                    sendIntent(this, LoginActivity.class);
                }
                showToast(msg);
                break;
            case HUCState.HUC_REQEUST_CODE_2:
                String msg1 = (String) response;
                if ("验证码已发送，请注意查收".equals(msg1)) {
                    myCountdownUtils = new MyCountdownUtils(btVerification, 60000, 1000);
                    myCountdownUtils.start();
                }
                showToast(msg1);
                break;
        }
        loadDismiss();
    }


}
